package com.mali.backendcommon.exception;

import com.mali.backendcommon.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 对非自定义异常进行处理
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Result exception(Exception e) {

        //记录异常
        log.error("系统异常{}", e.getMessage());

        //解析异常信息并返回
        return Result.error("服务太忙，请稍后再试或者联系管理员操作");
    }

    @ExceptionHandler(ShuMaException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Result customException(ShuMaException e) {

        //记录异常
        log.error("系统异常{}", e.getMessage());

        //解析异常信息并返回
        return Result.error(e.getMessage());
    }

}
